package com.grape.service.impl;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grape.dao.BookmarkRepository;
import com.grape.dao.Bookmark_Box_Repository;
import com.grape.dao.UserRepository;
import com.grape.pojo.Bookmark;
import com.grape.pojo.Bookmark_Box;
import com.grape.pojo.User;
import com.grape.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	private Bookmark_Box_Repository bbr;
	
	@Autowired
	private BookmarkRepository br;
	
	@Autowired
	private UserRepository ur;

	@Override
	public User login(String username, String password) {
		User user = ur.findByUsernameAndPassword(username,password);
		return user;
	}


	@Override	
	public void register(User model) {
		
		ur.save(model);
		model.setPictures("/time/parent-time/everyday/img/back(1).png-----"
						+ "/time/parent-time/everyday/img/back(2).png-----"
						+ "/time/parent-time/everyday/img/back(3).png-----"
						+ "/time/parent-time/everyday/img/back(4).png-----"
						+ "/time/parent-time/everyday/img/back(5).png-----"
						+ "/time/parent-time/everyday/img/back(6).png-----"
						+ "/time/parent-time/everyday/img/back(7).png-----"
						+ "/time/parent-time/everyday/img/back(8).png-----"
						+ "/time/parent-time/everyday/img/back(9).png-----"
						+ "/time/parent-time/everyday/img/back(10).png");
		
		Bookmark_Box bb = new Bookmark_Box("时间书签",model);
		bbr.save(bb);
		
		
		// 书签盒子关联书签
		List<Bookmark> bs = new ArrayList<>();
		bs.add(new Bookmark("年:百度","http://www.baidu.com/",bb));
		bs.add(new Bookmark("月:bilibili","https://www.bilibili.com/",bb));
		bs.add(new Bookmark("日:acfun","http://www.acfun.cn/",bb));
		bs.add(new Bookmark("时:LoL","http://lol.qq.com/",bb));
		bs.add(new Bookmark("分:校园网","http://172.21.200.1:8080/selfservice/",bb));
		bs.add(new Bookmark("秒:内涵段子","http://neihanshequ.com/",bb));
		br.save(bs);
		
		
	}


	@Override
	public void save(User user) {
		ur.save(user);
	}


	@Override
	// 当用户-书签盒子-书签	两次抓取策略都用EAGER时会出bug
	public User findOne(int i) {
		User user = ur.findOne(i);
		System.out.println(user);
		return user;
	}


	@Override
	public void setPrefer(User model) {
		User user = ur.findOne(model.getId());
		user.setPrefer(model.getPrefer());
	}


	@Override
	public User findByUsername(String username) {
		User user = ur.findByUsername(username);
		System.out.println(user);
		return user;
	}


	@Override
	public void delBg(User model, String bgSrc) {
		User user = ur.findOne(model.getId());
		String pictures = user.getPictures();
		
		if (bgSrc.equals(user.getPrefer())) {
			user.setPrefer("");
		}
		
		if (pictures.indexOf(bgSrc + "-----") == -1) {
			user.setPictures(pictures.replace("-----"+bgSrc,""));
		}else {
			user.setPictures(pictures.replace(bgSrc + "-----",""));
		}
		
		
		// 如果不是默认的,删除本地图片
		if (bgSrc.indexOf("/everyday/img/back") == -1) {
			String fileName = bgSrc.substring(bgSrc.indexOf(user.getUsername())+4,bgSrc.length());
			
			String path = ServletActionContext.getServletContext().getRealPath(File.separator + "user") + File.separator
			+ "private_img" + File.separator + user.getUsername() + File.separator + fileName;
			
			File file = new File(path);
			file.delete();
		}
	}


}
